﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ConfigurationManagement.Administration.Data;
using ConfigurationManagement.Data;
using System.Linq;
using System.ComponentModel;

namespace ConfigurationManagement.Administration.UI
{
    public partial class ManageSolutionDictionaryForm : Form
    {
        private readonly ConfigurationManagementDataContext _db = new ConfigurationManagementDataContext();
        private Project _selectedProject;
        private Solution _selectedSolution;

        public ManageSolutionDictionaryForm()
        {
            InitializeComponent();
        }

        private void SolutionsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            if (SolutionsComboBox.SelectedIndex < 0) return;
            Save();
            _selectedSolution = (Solution) SolutionsComboBox.SelectedItem;
            LoadDictionary();
        }

        private void ProjectsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProjectsComboBox.SelectedIndex < 0) return;
            Save();
            _selectedProject = (Project) ProjectsComboBox.SelectedItem;
            LoadSolutions();
        }

        private void ManageSolutionDictionaryFormLoad(object sender, EventArgs e)
        {
            if (DictionaryGridView != null)
            {
                DictionaryGridView.AutoGenerateColumns = false;
                
// ReSharper disable PossibleNullReferenceException
                DictionaryGridView.Columns["SettingTag"].DataPropertyName = "SettingTag";
// ReSharper restore PossibleNullReferenceException
// ReSharper disable PossibleNullReferenceException
                DictionaryGridView.Columns["DefaultValue"].DataPropertyName = "DefaultValue";
// ReSharper restore PossibleNullReferenceException
// ReSharper disable PossibleNullReferenceException
                DictionaryGridView.Columns["SettingDescription"].DataPropertyName = "SettingDescription";
// ReSharper restore PossibleNullReferenceException
                
            }
            LoadProjects();
        }

        private void ManageSolutionDictionaryFormFormClosing(object sender, FormClosingEventArgs e)
        {
            Save();
        }

        private void Save()
        {
            if (!DataContext.ContainsChanges(_db)) return;
            var dr = MessageBox.Show(@"Changes have been made, would you like to submit them?",
                                              @"Confirm changes", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
                _db.SubmitChanges();
        }

        private void LoadProjects()
        {
            ProjectsComboBox.DisplayMember = "ProjectName";
            ProjectsComboBox.ValueMember = "ProjectID";
            ProjectsComboBox.DataSource = _db.Projects.OrderBy(x => x.ProjectName);            
            _selectedProject = (Project) ProjectsComboBox.SelectedItem;
            LoadSolutions();
        }

        private void LoadSolutions()
        {
            if (_selectedProject == null) return;
            SolutionsComboBox.DisplayMember = "SolutionName";
            SolutionsComboBox.ValueMember = "SolutionID";
            var items = new List<Solution>(_selectedProject.Solutions.OrderBy(x => x.SolutionName));
            SolutionsComboBox.DataSource = items;
            _selectedSolution = (Solution) SolutionsComboBox.SelectedItem;
            LoadDictionary();
        }

        /// <summary>
        /// </summary>
        private void LoadDictionary()
        {
            DictionaryGridView.DataSource = _selectedSolution.SolutionSettings;
// ReSharper disable AssignNullToNotNullAttribute
            DictionaryGridView.Sort(DictionaryGridView.Columns["SettingTag"], ListSortDirection.Ascending);
// ReSharper restore AssignNullToNotNullAttribute
        }

        private void DictionaryGridViewUserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            var setting = (SolutionSetting) e.Row.DataBoundItem;
            if (setting.SettingValues.Count > 0)
            {
                var dr =
                    MessageBox.Show(
                        @"The dictionary item has environment settings configured, are you sure you want to delete it?",
                        @"Delete Dictionary Item", MessageBoxButtons.YesNo);
                if (dr != DialogResult.Yes)
                {
                    e.Cancel = true;
                    return;
                }
            }
            foreach (var val in setting.SettingValues)
                _db.SettingValues.DeleteOnSubmit(val);

            _db.SolutionSettings.DeleteOnSubmit(setting);
        }
    }
}